<?php

namespace App\Http\Controllers\Advertisement;

use App\Models\AdvToutiao;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;


class YiMaController extends Controller
{
    const ADVERTISE_SOURCE_TOUTIAO_GROUP = [
        self::ADVERTISE_SOURCE_TOU_TIAO,
        self::ADVERTISE_SOURCE_YI_MA,
        self::ADVERTISE_SOURCE_ZHONG_AN
    ];   

    const ADVERTISE_SOURCE_TOU_TIAO = 51001;//
    const ADVERTISE_SOURCE_YI_MA = 51002;//
    const ADVERTISE_SOURCE_ZHONG_AN = 51003;
    

    const ADVERTISE_SOURCE_CONFLICT = [
        self::ADVERTISE_SOURCE_TOU_TIAO=>'头条',
        self::ADVERTISE_SOURCE_YI_MA=>'亿玛',
        self::ADVERTISE_SOURCE_ZHONG_AN=>'众安'
    ];
    const USER_STATUS_CLICK = 0;//点击
    const USER_STATUS_ACTIVE = 1;//激活
    const USER_STATUS_REGISTER = 2;//注册
    const USER_STATUS_LOAN_REQUEST = 3;//授信

    public function advertise_yima_bind(Request $request) {
        $adid = $request->input('adid','');//广告计划 id 原值
        $cid = $request->input('creativeid','');//广告创意 id
        $ctype = $request->input('creativetype','');//创意样式 2=小图模式 3=大图式 4=组图模式 5=视频
        $phone_num = $request->input('phone_num','');//手机号
        $click_id = $request->input('clickid','');//落地页点击id
        $adv_toutiao = new AdvToutiao();
        try{
            if(empty($phone_num)){
                Log::debug("亿玛头条用户入库失败,原因:".$exception->getMessage().'请求参数:'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                return json_encode(['status'=>1,'msg'=>'phone_num is null']);
            }
            if(empty($click_id)){
                Log::debug("亿玛头条用户入库失败,原因:".$exception->getMessage().'请求参数:'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                return json_encode(['status'=>1,'msg'=>'clickid is null']);
            }
     
            $user_id = User::where('phone_num',$phone_num)->value('id');//用户id
            $has_advertise = $adv_toutiao::where([
                'user_id'=>$user_id,
                'click_id'=>$click_id
            ])->value('user_id');

            if (!empty($has_advertise)) {      
                Log::debug("头条用户转化失败：原因：重复添加，请求参数：".json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                return json_encode(['status'=>1,'msg'=>'user already exists']);
            }else {
                $data = [
                    'adid'=>$adid,
                    'cid'=>$cid,
                    'ctype'=>$ctype,
                    'source'=>self::ADVERTISE_SOURCE_YI_MA,
                    'user_id'=>$user_id,
                    'click_id'=>$click_id,
                    'user_status'=>self::USER_STATUS_REGISTER
                ];
                $res = AdvToutiao::create($data);
            }
        }catch (\Exception $exception) {
            Log::debug("亿玛头条用户入库失败,原因:".$exception->getMessage().'请求参数:'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
            return json_encode(['status'=>1,'msg'=>$exception->getMessage()]);
        }
        return json_encode(['status'=>0]);
    }

    private function send_toutiao_event($url='',$method='GET',$params=[]) {
        // return json_decode(['ret'=>0]);
//        $http_url = "http://kadai.in/api/adv/testGuzzleHttp";
        $http_url = $url.http_build_query($params);
        dd($http_url);
        $client = new Client();
        $response = $client->request($method, $http_url);
        return  json_decode($response->getBody()->getContents(), true);
    }



}
